#include "event/my_worker_processor.h"
#include "event/my_event_loop.h"

void my_test_dump_handle(my_event_t *event)
{
    int data = (int)(intptr_t)event->data;
    MY_DEF_LOG_DEBUG("timer_task timeout, data: %d", data);

    if (data < 50) {
        data += 10;
        event->data = (void *)(intptr_t)data;
        my_event_loop_add_timer(event, 5);
        return;
    }

    my_thread_free(event);
}

void my_test_timer_handler(my_dispatch_event_t *dispatch_event)
{
    int data = (int)(intptr_t)dispatch_event->data;
    my_thread_free(dispatch_event);

    my_event_t *timer_event = my_thread_calloc(sizeof(my_event_t));
    timer_event->handler = my_test_dump_handle;
    timer_event->data = (void *)(intptr_t)data;

    my_event_loop_add_timer(timer_event, 5);
}

void test_timer_task(int i)
{
    my_dispatch_event_t *dispatch_event =
        my_thread_calloc(sizeof(my_dispatch_event_t));
    dispatch_event->handler = my_test_timer_handler;
    dispatch_event->data = (void *)(intptr_t)i;
    my_worker_processor_post_event(dispatch_event);
}

int main()
{
    my_def_log_init(LOG_LEVEL_DEBUG, NULL, LOG_ROLLING_DAY);
    my_worker_processor_start(5, 1, 1);

    for (int i = 0; i < 20; i++) {
        test_timer_task(i);
    }

    my_worker_processor_stop();

    return 0;
}